/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/

/**
 *
 * $Id: AudioClient.hpp,v 1.3 2009-12-01 09:32:21 morsko Exp $
 *
 * \file
 * \brief MaCI - Audio Interface Client header
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 */
#ifndef _MACI_INTERFACE_AUDIOCLIENT_HPP_
#define _MACI_INTERFACE_AUDIOCLIENT_HPP_

#include "MaCI.hpp"
#include "thread.hpp"
#include "sync.hpp"

// These are not necessarily required here, but defined to make the
// interface easier to include.
#include "AudioData.hpp"
#include "AudioContainer.hpp"


// Forward declare gimi::GIMI
namespace gimi {
  class GIMI;
}

namespace MaCI {
  namespace Audio {
    
    // Forward declarations.
    class CAudioData;
    class CAudioClient;


    /** Callback interface class for Audio interface.
     *
     * This interface defines functions which can be used for
     * AudioClient callbacks. These functions are executed (more
     * detail per function basis) as new data arrives, and directly
     * from the context of the receiving thread. <b>Therefore, it is
     * important that the handling function is as simple and fast as
     * possible, as it will block the execution of the receiving
     * thread while executed.</b>
     */
    class CAudioClientCallback
    {
    public:

      CAudioClientCallback() {}
      virtual ~CAudioClientCallback() {}


      /** Handler function for AudioData event.
       *
       * This function is called when the AudioClient receives new
       * data from the server.
       *
       * \note <b>Keep this function simple & fast as it will block the 
       * data receiving thread while executing!</b>
       *
       * @param[in] aData       Constant reference to CAudioData element 
       *                        containing the received event.
       * @param[in] aDataSequence Sequence number of the received data. 
       *                        This should increment by one on each call.
       * @param[in] aAudioClient Reference to AudioClient 
       *                        instance which received this event.
       */
      virtual void OnAudioDataEvent(const CAudioData &aData, 
                                    const unsigned int aDataSequence,
                                    CAudioClient &aAudioClient) = 0;
    };


    /** MaCI - Audio Client interface.
     * 
     * This class contains the methods and types for using
     * a MaCI::Audio service in the GIMnet.
     */
    class CAudioClient : private gim::CSync,
                         private gim::CThread,
                         public MaCI::CMaCI
    {
    public:
      /** Constructor.
       * 
       * Constructor of the AudioClient. This requires;
       * a pointer to Initialized and connected GIMI, minor
       * number of this interface (Used as GIMI minor number)
       *
       * @param[in] aGIMIPtr Pointer to GIMI used for communication
       * @param[in] aInterfaceMinor Interface minor number. Usually
       *            used to indicate the instance of devices of same
       *            type.
       */     
      CAudioClient(gimi::GIMI *aGIMIPtr, const int aInterfaceMinor = -1);

      
      /** Destructor.
       */
      ~CAudioClient();



      /** Receive Audio data elements.
       *
       * This function returns CAudioData elements sent from the
       * service side of the interface. All data is packaged inside
       * the CAudioData container. Last received audio is stored
       * inside this AudioClient instance, so calling GetAudioData
       * multiple times with same parameters will always return the
       * same audio.
       *
       * @param[out] aImgData   Qualified CAudioData element is stored
       *                        to this reference on succesfull call.
       * @param[in] aImgSeq     Pointer to audiosequence number.
       *                        If this is set to NULL, the function will
       *                        return the last received audio immediately.
       *                        If this is not NULL, the value pointed
       *                        should be set to 0 on the first call to this 
       *                        function. After each call the sequence number 
       *                        of the returned scan is stored here. If user 
       *                        wants to read all audios available in sequence 
       *                        then this value should not be modified by user 
       *                        after the first call. Default value is NULL.
       * @param[in] aTimeout_ms Timeout in milliseconds to wait for
       *                        the operation to succeed. If no audio was
       *                        received during the timeout, error is returned.
       *                        Default timeout is 1000ms.
       * @return                'true' in case an audio was succesfully stored
       *                        on the provided reference. 'false' on any error,
       *                        including case 'no audios available' and 
       *                        'timeout'.
       */
      bool GetAudioData(CAudioData &aAudioData, 
                        unsigned int *aImgSeq = NULL,
                        const unsigned int aTimeout_ms = 1000);


      bool GetAudioInfo(TAudioInfo &aAudioInfo);

      /** Assigns the Audio client callback function. 
       *
       * This sets a function object for calling a audio client
       * callback. If this is set, the interface calls the assigned
       * functions immediately when event is available.
       * 
       * \note As the function is run in the context of the receiving
       * thread, user must make sure that the callback function is
       * fast. As if the function executes too long, receiving events
       * is interrupted.
       *
       * @param[in] aAudioClientCallback Pointer to class implementing the interface
       *                                 CAudioClientCallback.
       */
      void SetAudioClientCallback(CAudioClientCallback *aAudioClientCallback) {
        iAudioClientCallback = aAudioClientCallback;
      }
      
    protected:
      /** Enumeration for handling Wait() events.
       */
      enum EAudioEvent { 
        KEventNewAudio = 0,      ///< New audio received.
      };
      /** Open client interface.
       */
      EMaCIError DoOpen(void);
      
      
      /** Close client interface.
       */
      EMaCIError DoClose(void);

      /// Thread runner function. Is in charge of receiving events.
      int ThreadFunction(const int aThreadNumber);
      int ProcessMessage(const gimi::GIMIMessage &msg);
      bool GetAudioInfoFromServer(const unsigned int aTimeout_ms);
      //data.
      unsigned int iAudioSequence;
      CAudioData iLastAudioData;
      CAudioClientCallback *iAudioClientCallback;
      TAudioInfo iAudioInfo;
      
      // Funcs.
      CAudioClient(const CAudioClient &)
        : CSync(2,1),
          CThread(1),
          CMaCI(NULL, GIMI_PROTOCOL_MACI_AUDIO, 0, ""),
          iAudioSequence(0),
          iLastAudioData(),
          iAudioClientCallback(NULL),
          iAudioInfo() {}
      CAudioClient &operator=(const CAudioClient &) { return *this; }

    };


  }
}

#endif //_MACI_INTERFACE_AUDIOCLIENT_HPP
